package com.attendance.www.hr.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.ClosedBean;
import com.attendance.www.hr.beans.ClosedPageBean;
import com.attendance.www.hr.dao.mapper.ApprovalRecordMapper;
import com.attendance.www.hr.dao.mapper.AttendanceMapper;
import com.attendance.www.hr.dao.mapper.ClosedExtMapper;
import com.attendance.www.hr.dao.mapper.ClosedMapper;
import com.attendance.www.hr.dao.mapper.RequestMapper;
import com.attendance.www.hr.dao.mapper.RetroactiveMapper;
import com.attendance.www.hr.entitys.ApprovalRecord;
import com.attendance.www.hr.entitys.ApprovalRecordExample;
import com.attendance.www.hr.entitys.Attendance;
import com.attendance.www.hr.entitys.AttendanceExample;
import com.attendance.www.hr.entitys.Closed;
import com.attendance.www.hr.entitys.ClosedExample;
import com.attendance.www.hr.entitys.Request;
import com.attendance.www.hr.entitys.RequestExample;
import com.attendance.www.hr.entitys.Retroactive;
import com.attendance.www.hr.entitys.RetroactiveExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IClosedService;
@Service
public class ClosedServiceImpl implements IClosedService {

	@Resource
	private ClosedMapper closedDao;
	@Resource
	private ClosedExtMapper closedExtDao;
	@Resource
	private ApprovalRecordMapper approvalRecordDao;
	@Resource
	private RequestMapper requestDao;
	@Resource
	private RetroactiveMapper retroactiveDao;
	@Resource
	private AttendanceMapper attendanceDao;
	
	
	/**
	 * 查询封账设置
	 * @return
	 */
	@Override
	public ClosedPageBean getClosedPageList(Integer startNum,Integer pageCount){
		ClosedPageBean closedPageBean=new ClosedPageBean();
		closedPageBean.setClosedBeanList(closedExtDao.getClosedPageList(startNum,pageCount));
		closedPageBean.setPageTotal(closedExtDao.getClosedCount());
		return closedPageBean;
	}
	
	/**
	 * 验证封账是否存在
	 * @param closedBean
	 * @return
	 */
	@Override
	public boolean saveClosedVerify(ClosedBean closedBean){
		ClosedExample example=new ClosedExample();
		example.createCriteria()
		.andClosedMonthEqualTo(closedBean.getClosedMonth())
		.andClosedYearEqualTo(closedBean.getClosedYear());
		List<Closed> closedList=closedDao.selectByExample(example);
		if(closedList.size()>0){
			return true;
		}
		return false;
	}

	/**
	 * 新增封账时间
	 */
	@Override
	public boolean saveClosed(ClosedBean closedBean) {
		Closed closed=new Closed();
		Integer year = closedBean.getClosedYear();
		Integer month = closedBean.getClosedMonth();
		closed.setClosedYear(closedBean.getClosedYear());
		closed.setClosedMonth(closedBean.getClosedMonth());
		int record=closedDao.insertSelective(closed);
		if(record<=0){
			return false;
		}
		//更新封账时间段内的待审批(1)记录，状态改为4，“已封账”(1改成4)
		this.updateApprovalRecord(year, month, 1, 4);
		return true;
	}
	
	/**
	 * 删除封帐时间
	 */
	@Override
	public boolean deleteClosed(Integer closedId){
		//查询是否被删除
		Closed closed=closedDao.selectByPrimaryKey(closedId);
		if(closed==null){
			throw new ResultFaultException("0_00012");
		}
		Integer year = closed.getClosedYear();
		Integer month = closed.getClosedMonth();
		int record=closedDao.deleteByPrimaryKey(closedId);
		if(record<=0){
			return false;
		}
		//解封时，更新封账时间段内的已封账(4)的记录，状态改为1，“待审批”（4改成1）
		this.updateApprovalRecord(year, month, 4, 1);
		return true;
	}
	/**
	 * 新增或删除封账时，更新审批记录
	 * @param year
	 * @param month
	 * @param isApprove  初始审批记录的状态
	 * @param isApproveTo 改成的审批记录的状态
	 */
	private void updateApprovalRecord(Integer year,Integer month, Integer isApprove, Integer isApproveTo) {
		//审批记录idList
		List<Integer> requestIdList = closedExtDao.getRequestIdByClosed(isApprove, year, month);
		List<Integer> retroactiveIdList = closedExtDao.getRetroactiveIdByClosed(isApprove, year, month);
		//考勤记录
		List<Integer> attendanceIdList = closedExtDao.getAttendanceIdByClosed(isApprove, year, month);
		
		if (requestIdList.size()>0) {//有记录
			//更新审批记录，更新申请记录
			ApprovalRecordExample arExample = new ApprovalRecordExample();
			arExample.createCriteria().andRequestIdIn(requestIdList);
			ApprovalRecord ar = new ApprovalRecord();
			ar.setIsApprove(isApproveTo);
			approvalRecordDao.updateByExampleSelective(ar, arExample);
			
			RequestExample rExample = new RequestExample();
			rExample.createCriteria().andRequestIdIn(requestIdList);
			Request request = new Request();
			request.setApprovalState(isApproveTo);
			requestDao.updateByExampleSelective(request, rExample);
		}
		if (retroactiveIdList.size()>0) {
			//更新审批记录，更新申请记录
			ApprovalRecordExample arExample = new ApprovalRecordExample();
			arExample.createCriteria().andRetroactiveIdIn(retroactiveIdList);
			ApprovalRecord ar = new ApprovalRecord();
			ar.setIsApprove(isApproveTo);
			approvalRecordDao.updateByExampleSelective(ar, arExample);
			
			RetroactiveExample rExample = new RetroactiveExample();
			rExample.createCriteria().andRetroactiveIdIn(retroactiveIdList);
			Retroactive retroactive = new Retroactive();
			retroactive.setApprovalState(isApproveTo);
			retroactiveDao.updateByExampleSelective(retroactive, rExample);
		}
		//更新考勤记录
		if (attendanceIdList.size()>0) {
			AttendanceExample attendanceExample = new AttendanceExample();
			attendanceExample.createCriteria().andAttendanceIdIn(attendanceIdList);
			Attendance att = new Attendance();
			att.setIsRepaired(isApproveTo);
			att.setIsApprovaled(isApproveTo);
			attendanceDao.updateByExampleSelective(att, attendanceExample);
		}
	}

	/**
	 * 封账验证
	 * true 已封账
	 */
	@Override
	public boolean verifyClosed(Integer year, Integer month) {
		// 查询封账的年，月
		ClosedExample closedExample=new ClosedExample();
		closedExample.createCriteria()
		.andClosedYearEqualTo(year)
		.andClosedMonthEqualTo(month);
		List<Closed> closedList=closedDao.selectByExample(closedExample);
		if(closedList.size()>0){
			return true;
		}else{
			return false;
		}
	}

}
